<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="出库日期" prop="ckDate">
        <el-date-picker clearable
          v-model="queryParams.ckDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择出库日期">
        </el-date-picker>
      </el-form-item>-->
      <el-form-item label="单号" prop="danhao">
        <el-input
          v-model="queryParams.danhao"
          placeholder="请输入出库单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出入日期" prop="beginTime">
        <el-date-picker clearable
                        v-model="queryParams.beginTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择开始日期">
        </el-date-picker>
        --
      </el-form-item>
      <el-form-item prop="endTime">
        <el-date-picker clearable
                        v-model="queryParams.endTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.crk_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!--<el-form-item label="仓库编号" prop="stockId">
        <el-input
          v-model="queryParams.stockId"
          placeholder="请输入仓库编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
     <!-- <el-form-item label="仓库名称" prop="stockName">
        <el-input
          v-model="queryParams.stockName"
          placeholder="请输入仓库名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
 <!--     <el-form-item label="出库时间" prop="cukuDate">
        <el-date-picker clearable
          v-model="queryParams.cukuDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择出库时间">
        </el-date-picker>
      </el-form-item>-->
      <el-form-item label="出库类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择出库类型" clearable>
          <el-option
            v-for="dict in dict.type.chuku_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:chukumain:add']"
        >添加出库</el-button>
      </el-col>
<!--      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:chukumain:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:chukumain:remove']"
        >删除</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:chukumain:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="chukumainList" @selection-change="handleSelectionChange">

<!--
      <el-table-column type="selection" width="55" align="center" />
-->

      <el-table-column label="出库单号" width="150" align="center" prop="danhao">
        <template scope="scope">
          <div style="cursor:pointer;text-decoration:underline;color: blue" class="danhao" @click="handleChuKuChaKan(scope.row)">{{ scope.row.danhao }}</div>
        </template>
      </el-table-column>
       <!-- <template scope="scope">
          <div style="cursor:pointer;text-decoration:underline;color: blue" class="id" @click="rowClick(scope.row)">{{ scope.row.id }}</div>
        </template>
      </el-table-column>-->

      <el-table-column label="出库日期" align="center" prop="ckDate" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.ckDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="出库总数量" align="center" prop="totalNum" />
      <el-table-column label="出库总价格" align="center" prop="totalPrice" />
      <el-table-column label="状态" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.crk_state" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="出库类型" width="100" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.chuku_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
   <!--   <el-table-column label="仓库编号" align="center" prop="stockId" />
      <el-table-column label="仓库名称" align="center" prop="stockName" />-->
      <el-table-column label="领料人" align="center" prop="llName" />
      <el-table-column label="经办人" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180"/>
      <el-table-column label="回收数量" align="center" prop="hsNum" />
      <el-table-column label="回收物料号" align="center" prop="hsItemCode" />
      <el-table-column label="回收规格型号" align="center" prop="hsSpec" />
      <el-table-column label="备注" show-overflow-tooltip align="center" prop="remark" />
      <el-table-column label="操作"   width="150" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- <el-button
             size="mini"
             type="text"
             icon="el-icon-search"
             @click="handleChuKuChaKan(scope.row)"
           >查看</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-circle-check"
            v-if="scope.row.state != 1"
            @click="handleChuKuSelect(scope.row)"

            v-hasPermi="['business:chukumain:zhixing']"
          >执行</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-if="scope.row.state != 1"
            v-hasPermi="['business:chukumain:remove']"
          >撤销</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="1400px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" style="margin-top: -20px" label-width="120px">
      <el-row>
        <el-col :span="8">
          <el-form-item label="出库类型" prop="type">
            <el-select v-model="form.type" placeholder="请选择出库类型" clearable>
              <el-option
                v-for="dict in dict.type.chuku_type"
                :key="dict.value"
                :label="dict.label"
                :value="parseInt(dict.value)"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="出库日期" prop="ckDate">
            <el-date-picker clearable
                            v-model="form.ckDate"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="请选择出库日期">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="领料人" prop="llName">
            <el-input style="width: 70%" v-model="form.llName" placeholder="请输入领料人" />
          </el-form-item>
        </el-col>
      </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="回收数量" prop="hsNum">
              <el-input style="width: 70%" v-model="form.hsNum" placeholder="请输入回收数量" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="回收物料号" prop="hsItemCode">
              <el-input style="width: 70%" v-model="form.hsItemCode" placeholder="请输入回收物料号" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="回收规格型号" prop="hsSpec" >
              <el-input style="width: 70%"  v-model="form.hsSpec" placeholder="请输入回收规格型号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
        <el-col :span="12">
          <el-form-item label="备注" prop="remark">
            <el-input style="width: 500px"  type="textarea" v-model="form.remark" placeholder="请输入备注" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <ChukuDetail ref="chukuDetail"></ChukuDetail>
        </el-col>
      </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">提 交</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :close-on-click-modal="false" :title="chukuSelect.title" :visible.sync="chukuSelect.open" width="1100px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" style="margin-top: -20px" label-width="80px">
       <!-- <el-row>
          <el-col :span="8">
            <el-form-item label="入库类型" prop="rukuType">
              <el-select disabled  v-model="form.rukuType" placeholder="请选择入库类型" clearable>
                <el-option
                  v-for="dict in dict.type.ruku_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                  :disabled="dict.value == 0"
                />
              </el-select>
            </el-form-item>
            <el-col :span="8">
              <el-form-item  label="入库日期" prop="rukuDate">
                <el-date-picker readonly clearable
                                v-model="form.rukuDate"
                                type="date"
                                value-format="yyyy-MM-dd"
                                placeholder="出库日期">

                </el-date-picker>
              </el-form-item>
            </el-col>
          </el-col>
          <el-col :span="8">
            <el-form-item label="仓库" prop="stockId">
              <el-select disabled @change="getCangku"  v-model="form.stockId" placeholder="请选择入库仓库" clearable>
                <el-option
                  v-for="dict in warehouseinfo"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input readonly style="width: 500px"  type="textarea" v-model="form.remark" placeholder="" />
            </el-form-item>
          </el-col>
        </el-row>-->
        <el-row>
          <el-col :span="24">
            <ChukuSelect ref="chukuSelect" :rowData ="this.rowData"></ChukuSelect>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" v-show="chukuSelect.isShow" @click="submitZx">执 行</el-button>
        <el-button @click="chukuSelect.open = false">取 消</el-button>
      </div>
    </el-dialog>
    <!--    出库单详情-->
    <el-dialog :close-on-click-modal="false" :title="chukuTitle" :visible.sync="chukuOpen"  width="1430px" append-to-body>
      <el-table v-loading="chukuLoading" :data="chukuList">
        <el-table-column label="出库单号" width="150" align="center" prop="danhao"  />
        <el-table-column label="出库日期" width="150" align="center" prop="cukuDate"  />
        <el-table-column label="物料名称" width="150" align="center" prop="itemName"  />
        <el-table-column label="物料编号" width="100" align="center" prop="itemCode"  />
        <el-table-column label="大类" width="100" align="center" prop="className"  />
        <el-table-column label="规格" width="100" align="center" prop="spec"  />
        <el-table-column label="单位" width="80" align="center" prop="unit"  />
        <el-table-column label="出库数量" width="80" align="center" prop="ckNum"  />
        <el-table-column label="库存数量" width="80" align="center" prop="stockNum"  />
        <el-table-column label="仓库" width="120" align="center" prop="stockName"  />
        <el-table-column label="供应商" width="120" align="center" prop="supplyName"  />
        <el-table-column label="经办人" width="150" align="center" prop="createBy"  />
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import { listChukumain, getChukumain, delChukumain, addChukumain, updateChukumain,zhixingChuku } from "@/api/business/chukumain";
import ChukuDetail from './chukuDetail';
import ChukuSelect from "./chukuSelect";
import { listChukudetail } from "@/api/business/chukudetail";

export default {
  components: {
    ChukuSelect,
    ChukuDetail
  },
  name: "chukuMain",
  dicts: ['crk_state', 'chuku_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      danhaos: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 出库单据主表表格数据
      chukumainList: [],
      chukuSelect:{
        // 是否显示弹出层（
        open: false,
        // 弹出层标题
        title: "",
        isShow:false,
      },
      // 弹出层标题
      title: "",
      chukuTitle:'',
      chukuOpen:false,
      chukuList:[],
      rowData:{},
      chukuLoading:true,
      // 是否显示弹出层
      open: false,
      openInfo: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        chukuFlag: null,
        ckDate: null,
        state: null,
        stockId: null,
        stockName: null,
        cukuDate: null,
        type: null,
        beginTime:null,
        endTime:null,
      },
      queryParams2: {
        pageNum: 1,
        pageSize: 10,
        mainId: null,
        ckNum: null,
        cukuDate: null,
        itemName: null,
        itemCode: null,
        supplyId: null,
        supplyName: null,
        stockId: null,
        stockName: null,
        classId: null,
        className: null,
        danhao:null
      },
      // 表单参数
      form: {
        infoList:[]
      },
      // 表单校验
      rules: {
        type: [
          { required: true, message: "请选择出库类型", trigger: "blur" }
        ],
        llName: [
          { required: true, message: "请输入领料人", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 点击订单号展示详情
    rowClickInfo(row){
      this.queryParams2.danhao = row.danhao
      listChukudetail(this.queryParams2).then(res=>{
        this.chukuList = res.rows
      })
      this.chukuTitle = '原材料出库单详情'
      this.chukuOpen = true
      this.chukuLoading = false
    },
    /** 查询出库单据主表列表 */
    getList() {
      this.loading = true;
      listChukumain(this.queryParams).then(response => {
        this.chukumainList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        ckDate: new Date(),
        state: null,
        stockId: null,
        stockName: null,
        cukuDate: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        type: null
      };
      this.resetForm("form");
    },

    //展示子表
    rowClick(row){
      let id = row.id;
      // console.log(this.$refs.formData)
      // this.$refs.formData.resetFields();
      this.reset();
      this.openInfo = true;
      this.title = "原材料出库详情";
      setTimeout(() => {
        this.$refs.indexInfo.listChukudetail(id);
      }, 5);

    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.danhaos = selection.map(item => item.danhao)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "原材料添加出库";
      this.form.type=3
      setTimeout(() => {
        this.$refs.chukuDetail.listReset();
      }, 10);
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      const danhaos = row.danhao || this.danhaos
      getChukumain(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改原材料出库单据";
        setTimeout(() => {
          this.$refs.chukuDetail.getCkInfoList(danhaos.toString());
        }, 5);
      });
    },
    /** 出库单查看 */
    handleChuKuChaKan(row) {
      this.reset();
      const id = row.id || this.ids
      const danhaos = row.danhao || this.danhaos
      this.rowData = row;
      getChukumain(id).then(response => {
        this.form = response.data;
        this.chukuSelect.open = true;
        this.chukuSelect.isShow=false;
        // this.chukuSelect.title = "出库单查看";
        this.chukuSelect.title = "出库单";
        setTimeout(() => {
          this.$refs.chukuSelect.getCkSelectList(danhaos.toString());
        }, 5);
      });
    },
    handleChuKuSelect(row) {
      this.reset();
      const id = row.id || this.ids
      const danhaos = row.danhao || this.danhaos
      getChukumain(id).then(response => {
        this.form = response.data;
        this.chukuSelect.open = true;
        this.chukuSelect.isShow=true;
        this.chukuSelect.title = "原材料出库单执行出库";
        setTimeout(() => {
          this.$refs.chukuSelect.getCkSelectList(danhaos.toString());
        }, 5);
      });
    },
    /*执行入库*/
    submitZx(){
      const danhao=this.form.danhao;
      this.$modal.confirm('是否确认执行原材料出库单为'+danhao+"吗?").then(function() {
        return zhixingChuku(danhao);
      }).then(() => {
        this.chukuSelect.open=false;
        this.getList();
        this.$modal.alertSuccess("出库成功");
      }).catch(() => {});
    },
    /** 提交按钮 */
    submitForm() {
      let chukudetailList= this.$refs.chukuDetail.chukudetailList;
      this.form.infoList=chukudetailList;
      if(chukudetailList.length==0){
        this.$modal.alertWarning('请选择需要出库的物料!')
        return;
      }
      for(var i=0;i<chukudetailList.length;i++){
        if(chukudetailList[i].ckNum==''||chukudetailList[i].ckNum==null||chukudetailList[i].ckNum==undefined){
          this.$modal.alertWarning('出库数量不能为空')
          return;
        }
      /*  if(chukudetailList[i].unitPrice==''||chukudetailList[i].unitPrice==null||chukudetailList[i].unitPrice==undefined){
          this.$modal.alertWarning('单价不能为空')
          return;
        }*/
        if(chukudetailList[i].ckNum>chukudetailList[i].stockNum){
          this.$modal.alertWarning('出库数量不能大于可用库存数量')
          return;
        }
      }
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.chukuFlag=1;
          if (this.form.id != null) {
            updateChukumain(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addChukumain(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const danhao = row.danhao
      if(row.state=='1'){
        this.$modal.alertWarning("原材料出库单"+danhao+"已完成无法撤销！");
        return;
      }
      this.$modal.confirm('是否确认撤掉原材料出库单为"' + danhao + '"？').then(function() {
        return delChukumain(danhao);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("撤销成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/chukumain/export', {
        ...this.queryParams
      }, `chukumain_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
